//
//  ClassifyDetailViewController.m
//  网络FM
//
//  Created by dengzhicheng on 15/5/22.
//  Copyright (c) 2015年 dengzhicheng.com. All rights reserved.
//

#import "ClassifyDetailViewController.h"
#import "ClassifyTableViewCell.h"
#import <AFNetworking.h>
#import "ClassifyModel.h"
#import <UIImageView+WebCache.h>
#import <MJRefresh.h>
#import "PlayerViewController.h"






@interface ClassifyDetailViewController ()<UITableViewDataSource, UITableViewDelegate>

@property (nonatomic, retain) UITableView *table;
@property (nonatomic, retain) NSMutableArray *tableArray;
@property (nonatomic, assign) NSInteger pageChanged;


@end

@implementation ClassifyDetailViewController

- (void)dealloc
{
    [_tableArray release];
    [_classify release];
    [_table release];
    [super dealloc];
}

- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
        self.tableArray = [NSMutableArray array];
        self.pageChanged = 2;
    }
    return self;
}

- (void)loadView
{
    [super loadView];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self startConnect];
    [self createTable];
    [self.view setBackgroundColor:[UIColor cyanColor]];
    self.navigationController.navigationBar.translucent = NO;
    
}

- (void)startConnect
{
    NSString *url = @"http://s.ajmide.com/catebig_search.php";
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    NSString *str = [NSString stringWithFormat:@"%@?%@", url, self.classify];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [dic objectForKey:@"data"];
        for (NSDictionary *dic in array) {
            ClassifyModel *model = [[ClassifyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
            [model release];
        }
        [self.table reloadData];
        
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
    }];
}

- (void)createTable
{
    self.table = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height - 64) style:UITableViewStylePlain];
    [self.table setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    self.table.rowHeight = 120;
    self.table.dataSource = self;
    self.table.delegate = self;
    [self.table addLegendHeaderWithRefreshingTarget:self refreshingAction:@selector(Refresh)];
    [self.table addLegendFooterWithRefreshingTarget:self refreshingAction:@selector(Load)];
    [self.view addSubview:_table];
    [_table release];
}

//下拉刷新
- (void)Refresh
{
    [self.table reloadData];
    [self.table.header endRefreshing];
}

//上拉加载
- (void)Load
{
    
    AFHTTPRequestOperationManager *manager = [AFHTTPRequestOperationManager manager];
    
    NSArray *array2 = [self.classify componentsSeparatedByString:@"&"];
    NSString *classStr = [array2 firstObject];
    
    NSString *str = [NSString stringWithFormat:@"http://s.ajmide.com/catebig_search.php?%@&page=%ld", classStr, _pageChanged];
    [manager GET:str parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
        NSData *data = operation.responseData;
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        NSMutableArray *array = [dic objectForKey:@"data"];
        if (array.count == 0) {
            [self.table removeFooter];
            return ;
        }
        for (NSDictionary *dic in array) {
            ClassifyModel *model = [[ClassifyModel alloc] init];
            [model setValuesForKeysWithDictionary:dic];
            [self.tableArray addObject:model];
            [model release];
        }
        [self.table reloadData];
    } failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        
    }];
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.tableArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *cellIdentifier = @"cell";
    ClassifyTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
    if (!cell) {
        cell = [[ClassifyTableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
    }
    ClassifyModel *model = [[ClassifyModel alloc] init];
    model = [self.tableArray objectAtIndex:indexPath.row];
    
    [cell.aImage sd_setImageWithURL:[NSURL URLWithString:model.imgPath] placeholderImage:[UIImage imageNamed:@"占位图.jpg"] options:SDWebImageRetryFailed];
    cell.nameLabel.text = model.name;
    [cell.likeButton setImage:[UIImage imageNamed:@"收藏图标.png"] forState:UIControlStateNormal];
    [cell.likeButton setTitle:model.totalFans forState:UIControlStateNormal];
    [cell.authorImage setImage:[UIImage imageNamed:@"主播图标.png"]];
    [cell.authorLabel setText:model.presenter];
    [cell.introduceLabel setText:model.intro];
    
    
    return cell;
}

// 点击cell,传递音频文件地址,跳转到播放界面
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    PlayerViewController *player = [[PlayerViewController alloc] init];
    ClassifyModel *model = [self.tableArray objectAtIndex:indexPath.row];
    player.urlForPlay = model.liveUrl;
    [self presentViewController:player animated:YES completion:nil];
    [player release];
}







@end
